package com.vitabuybuy.tv.ui.auth

import android.os.Bundle
import android.os.CountDownTimer
import android.view.View
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.ActivityRegisterBinding
import com.library.common.base.BaseActivity
import com.library.common.base.MessageModel
import com.library.common.ext.setOnclickNoRepeat
import com.library.common.toast.XToast
import com.vitabuybuy.tv.dialog.AreaCodePopup
import com.vitabuybuy.tv.ui.WebViewActivity
import com.vitabuybuy.tv.ui.auth.viewmodel.AuthViewModel
import kotlinx.android.synthetic.main.activity_find_password.*
import kotlinx.android.synthetic.main.activity_register.*
import kotlinx.android.synthetic.main.activity_register.countryCodePicker
import kotlinx.android.synthetic.main.activity_register.etPhone
import kotlinx.android.synthetic.main.activity_register.etSmsCode
import kotlinx.android.synthetic.main.activity_register.tvGetSmsCode

class RegisterActivity : BaseActivity<AuthViewModel, ActivityRegisterBinding>() {
    override val isUseBaseTitleBar: Boolean
        get() = true
    override val layoutId: Int
        get() = R.layout.activity_register

    private var isGetCode = false
    private var defaultAreaCode = "86"

    override fun initView(savedInstanceState: Bundle?) {
        setTitle(getString(R.string.register))
    }

    override fun eventHandling() {
        setOnclickNoRepeat(tvGetSmsCode, tvUserAgreement, tvPrivacyPolicy, btnRegister, countryCodePicker){v->
            when (v) {
                tvGetSmsCode -> {
                    when {
                        etPhone.text.isEmpty() -> XToast.normal(getString(R.string.enter_phone_number))
//                    !RegexUtils.isMobileExact(etPhone.text) -> XToast.normal(getString(R.string.enter_correct_phone_number))
                        else -> viewModel.sendToPhone(
                            "",
                            etPhone.text.toString(),
                            defaultAreaCode
                        )
                    }
                }
                tvUserAgreement -> {
                    WebViewActivity.openActivity(mContext, "", "", 11)
                }
                tvPrivacyPolicy -> {
                    WebViewActivity.openActivity(mContext, "", "", 12)
                }
                btnRegister -> {
                    when {
                        !isGetCode -> XToast.normal(getString(R.string.please_get_code))
                        etSmsCode.text.isEmpty() -> XToast.normal(getString(R.string.enter_code))
                        else -> viewModel.validateByPhone(
                            etPhone.text.toString(),
                            etSmsCode.text.toString(),
                            defaultAreaCode
                        )
                    }
                }
                countryCodePicker -> {
                    AreaCodePopup(this) {
                        countryCodePicker.text = it
                        defaultAreaCode = it.replace("+", "")
                    }.showPopupWindow()
                }
            }
        }
    }

    override fun handleEvent(msg: MessageModel) {
        super.handleEvent(msg)
        when (msg.code) {
            1 -> {
                XToast.success(getString(R.string.already_sms_code))
                timer.start()
            }
            2 -> {
                SetPasswordActivity.openActivity(
                    mContext,
                    defaultAreaCode,
                    etPhone.text.toString()
                )
            }
        }
    }

    private val timer = object : CountDownTimer((60 * 1000).toLong(), 1000) {
        override fun onTick(millisUntilFinished: Long) {
            isGetCode = true
            mBinding.tvGetSmsCode.text = String.format(
                getString(R.string.get_sms_code_s),
                millisUntilFinished / 1000
            )
            mBinding.tvGetSmsCode.isEnabled = false
        }

        override fun onFinish() {
            mBinding.tvGetSmsCode.text = getString(R.string.get_sms_code)
            mBinding.tvGetSmsCode.isEnabled = true
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        timer.cancel()
    }

    companion object {
        fun openActivity(mContext: BaseActivity<*, *>) {
            mContext.startActivity(null, RegisterActivity::class.java)
        }
    }
}
